package net.easyjava.tools.io;

import net.easyjava.tools.BaseTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by wxnacy on 16/2/2.
 */
public class InputStreamTools extends BaseTools {
    private static Logger logger = LoggerFactory.getLogger(InputStreamTools.class);
    /**
     * 从 输入流获取byte[]
     * @param inputStream
     * @return
     */
    public static byte[] getByteByInputStream(InputStream inputStream){
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024]; //buff用于存放循环读取的临时数据
        int rc = 0;
        try {
            while (inputStream.read(buff) > 0) {
                swapStream.write(buff);
            }
            byte[] data = swapStream.toByteArray();
            return data;
        } catch (IOException e) {
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }
    }
}
